package com.kaili.basic.service.impl;

import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.kaili.basic.mapper.BaseStoreStockMapper;
import com.kaili.basic.pojo.dto.BaseStoreStockQueryDto;
import com.kaili.basic.pojo.entity.*;
import com.kaili.basic.pojo.vo.BaseStoreStockVO;
import com.kaili.basic.service.BaseStoreStockService;
import com.kaili.common.util.LoginDetailUtils;
import com.kaili.common.util.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class BaseStoreStockServiceImpl extends ServiceImpl<BaseStoreStockMapper, BaseStoreStockEntity> implements BaseStoreStockService {

    @Autowired
    private BaseStoreStockMapper baseStoreStockMapper;

    @Override
    public void bathInsert(List<?> detailList,Long supplierId) {
        List<BaseStoreStockEntity> stockList=new ArrayList<>();
        for (Object obj:detailList) {
            BaseStoreStockEntity stockEntity=new BaseStoreStockEntity();
            BaseStoreDetailEntity detail=null;
            //入库管理
            if(obj instanceof BaseInStoreDetailEntity){
                detail=(BaseInStoreDetailEntity)obj;
                //移库管理
            }else if(obj instanceof BaseMoveStoreDetailEntity){
                detail=(BaseMoveStoreDetailEntity)obj;
                stockEntity.setStoreId(((BaseMoveStoreDetailEntity) detail).getStoreId());
                stockEntity.setLocationId(((BaseMoveStoreDetailEntity) detail).getDestLocationId());
                //报损管理
            }else if(obj instanceof BaseReportLossDetailEntity){
                detail=(BaseReportLossDetailEntity)obj;
                stockEntity.setStoreId(((BaseReportLossDetailEntity) detail).getStoreId());
                stockEntity.setLocationId(((BaseReportLossDetailEntity) detail).getLossLocationId());
                //调拨管理
            }else if(obj instanceof BaseAllottedStoreDetailEntity){
                detail=(BaseAllottedStoreDetailEntity)obj;
                stockEntity.setStoreId(((BaseAllottedStoreDetailEntity) detail).getDestStoreId());
                stockEntity.setLocationId(((BaseAllottedStoreDetailEntity) detail).getDestLocationId());
            }else{
                new RuntimeException("新增库存类型转换异常");
            }
            BeanUtils.copyProperties(detail,stockEntity);
            stockEntity.setInStoreDetailId(detail.getId());
            stockEntity.setRawAmount(detail.getAmount());
            stockEntity.setCurrAmount(detail.getAmount());
            stockEntity.setCurrWeight(detail.getWeight());
            stockEntity.setCurrVo(detail.getVolume());
            stockEntity.setCurrTrays(detail.getTrays());
            stockEntity.setSupplierId(supplierId);
            stockEntity.setCreateTime(new Date());
            stockEntity.setCreateBy(LoginDetailUtils.getCurrentEmployeeId().longValue());
            stockList.add(stockEntity);
        }
       this.insertBatch(stockList);
    }

    @Override
    public Boolean updateStock(List<?> detailList) {
        for (Object obj:detailList) {
            BaseStoreDetailEntity entity=null;
            if(obj instanceof BaseMoveStoreDetailEntity){
                entity=(BaseMoveStoreDetailEntity)obj;
            }else if(obj instanceof BaseReportLossDetailEntity){
                entity=(BaseReportLossDetailEntity)obj;
            }else if(obj instanceof BaseOutStoreDetailEntity){
                entity=(BaseOutStoreDetailEntity)obj;
            }else if(obj instanceof BaseAllottedStoreDetailEntity){
                entity=(BaseAllottedStoreDetailEntity)obj;
            }
            BaseStoreStockEntity storeStock= this.selectById(entity.getStoreStockId());
            //判断库存是否足够
            if(storeStock.getCurrAmount()<entity.getAmount()){
                return false;
            }
            //削减库存
            storeStock.setCurrAmount(storeStock.getCurrAmount()-entity.getAmount());
            storeStock.setCurrVo(storeStock.getCurrVo()-entity.getVolume());
            storeStock.setCurrTrays(storeStock.getCurrTrays()-entity.getTrays());
            storeStock.setCurrWeight(storeStock.getCurrWeight()-entity.getWeight());
            this.updateById(storeStock);
        }
        return true;
    }

    @Override
    public Page<BaseStoreStockVO> queryCanOutStock(Page<BaseStoreStockVO> page, BaseStoreStockQueryDto queryDto) {
        List<BaseStoreStockVO> list = baseStoreStockMapper.queryCanOutStock(page, queryDto);
        page.setRecords(list);
        return page;
    }

    @Override
    public List<BaseStoreStockEntity> queryStockBySupplierId(Map<String, Object> map) {
        return baseStoreStockMapper.queryStockBySupplierId(map);
    }

//    @Override
//    public List<BaseStoreStockEntity> queryStockGroupByLocation(Map<String, Object> map) {
//        return baseStoreStockMapper.queryStockGroupByLocation(map);
//    }
}
